﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using RescueApp.Models;

namespace RescueApp.Data
{
    public class RescueDispatchConfig : IEntityTypeConfiguration<RescueDispatch>
    {
        public void Configure(EntityTypeBuilder<RescueDispatch> rescueDispatch)
        {
            rescueDispatch.ToTable("RescueDispatch");
            rescueDispatch.HasKey(d => d.DispatchID);
            //rescueDispatch.Property(d => d.DispatchID).ValueGeneratedOnAdd(); // 主键由数据库自动生成
            rescueDispatch.Property(d => d.DispatchDriver).IsRequired().HasMaxLength(20);
            rescueDispatch.Property(d => d.DispatchVehicleLicense).IsRequired().HasMaxLength(10);
            rescueDispatch.Property(d => d.ArrivalTime).IsRequired();
            rescueDispatch.Property(d => d.MalfunctionCause).HasMaxLength(200);
            rescueDispatch.Property(d => d.RescueCost).IsRequired();

            rescueDispatch.HasOne(d => d.RescueApply)
                .WithMany(a => a.RescueDispatches)
                .HasForeignKey(d => d.RescueID)
                .OnDelete(DeleteBehavior.Restrict);
        }
    }
}